const fs = require('fs')
const path = require('path')
const { exec, spawn } = require('child_process')

const huskyDir = path.resolve(__dirname, '..', '.husky', '_')
const projectDir = path.resolve(__dirname, '..')

const command = process.argv.slice(2)

if (!fs.existsSync(huskyDir)) {
  exec('husky', { cwd: projectDir }, (error) => {
    if (error) {
      console.error(error)
      return
    }
    run()
  })
} else {
  run()
}

function run() {
  spawn(`npx`, command, { cwd: projectDir, shell: true, stdio: 'inherit' })
}
